function bindSidebarEvent(){

    $(".main-sidebar li a").click(function(e){

        //获取当前菜单
        var $this = $(this);

        //获取菜单的下一个元素
        var $menu = $this.next();
        //判定下一个元素是否子菜单
        if($menu.is('.treeview-menu')){
            if ( $menu.is(':visible')) {
                //关闭子菜单
                $menu.slideUp(500, function () {
                    $menu.removeClass('menu-open');
                   /* $$.layout();*/
                });
                //父菜单失去激状态
                $menu.parent("li").removeClass("active");
            }else{
                //找到父元素
                var parent = $this.parents('ul').first();
                //关闭所有已打开同级子菜单
                var ul = parent.find('ul:visible').slideUp(500);
                //删除所有已打开同级子菜单的打开样式
                ul.removeClass('menu-open');
                //找到子菜单
                var parentLi = $this.parent("li");
                //打开子菜单
                $menu.slideDown(500, function () {
                    //添加其他子菜单打开样式
                    $menu.addClass('menu-open');
                    //删除同级子菜单的激活状态
                    parent.find('li.active').removeClass('active');
                    //激活子菜单
                    parentLi.addClass('active');
                    //调整布局
                    /*$$.layout();*/
                });
            }
        }
        if($this.attr("menuCode")) {
            e.preventDefault();
            $$.goUrl($this.attr("href"));
        }
    });
}

function initSiderbarTree($ul,menuDatas,level){
    if(menuDatas && menuDatas.length){
        for(var i = 0; i<menuDatas.length; i++){
            var menuData = menuDatas[i];
            var menuCode = menuDatas[i].menuCode;
            var status = window.menuCode == menuCode
            var  bchildren = false;
            if(menuData.children && menuData.children){
                bchildren = true;
            }
            if(menuData.menuPath || bchildren){
                var leafHtml = [];
                //添加三级菜单的链接信息
                leafHtml.push("<a  href='");
                if(menuData.menuPath){
                    leafHtml.push(menuData.menuPath);
                    leafHtml.push("' ");
                    if(status){
                        leafHtml.push("'class='active'")
                    }
                }else{
                    leafHtml.push("#'");
                }
                if(menuData.menuCode){
                    leafHtml.push(" menuCode='"+menuData.menuCode+"' ");
                }else{
                    leafHtml.push(" ");
                }

                leafHtml.push(">");
                if(menuData.menuIcon){
                    leafHtml.push("<i class='");
                    leafHtml.push(menuData.menuIcon);
                    leafHtml.push("'></i>");
                }else{
                    if(level && !bchildren){
                        leafHtml.push("<i class='fa fa-lg fa-circle-o'></i>");
                    }else{
                        leafHtml.push("<i class='fa fa-lg fa-folder'></i>");
                    }
                }
                leafHtml.push("<span>");
                leafHtml.push(menuData.menuName);
                leafHtml.push("</span>");
                if(menuData.menuRight){
                    leafHtml.push("<span class='label pull-right menu-label-bg'>");
                    leafHtml.push(menuData.menuRight);
                    leafHtml.push("</span>");
                }else{
                    if(bchildren){
                        leafHtml.push("<i class='glyphicon glyphicon-chevron-left pull-right'></i>");
                    }
                }
                leafHtml.push("</a>");
                var $left = $(leafHtml.join(""));
                if(bchildren){
                    var $li = $("<li class='treeview'></li>");
                    var $cul = $("<ul class='treeview-menu'></ul>");
                    $li.append($left);
                    $li.append($cul);
                    initSiderbarTree($cul,menuData.children,level+1);
                    $ul.append($li);
                }else{
                    var $li = $("<li ></li>") ;
                    $li.append($left);
                    $ul.append($li);
                }
            }
        }
    }
}

function initSiderbarHtml(menuDatas){
    $sidebar = $(".main-sidebar");
    $sidebarUl = $(" <ul class='sidebar-menu'></ul>");
    $sidebar.append($sidebarUl);
    initSiderbarTree($sidebarUl,menuDatas,0);
    //alert($sidebar.html());
    bindSidebarEvent();
    setTimeout(function(){
        //选中左侧菜单
        if(window.thirdMenuCode){
            $("a[menuCode="+window.thirdMenuCode+"]").addClass("active")
            $("a[menuCode="+window.thirdMenuCode+"]").parent().parent().parent().children("a:first").click();

        }
    },200);
}

function initRemoteSiderbarDataByMenuId(topMenuId){
	$$.ajax({
        url:"/rest/BaseMenuBusiness/getUserLeftMenuByTopMenuId",
        data : {
            topMenuId : topMenuId
        },
        success : function(data){
             menuDatas=data;
             $sidebar = $(".main-sidebar");
             $sidebar.empty();
             initSiderbarHtml(menuDatas);
             var storage = window.localStorage;
			if(storage){
				storage.setItem("hgtg-"+topMenuId,JSON.stringify(menuDatas));
			}
        }
    });
}
function initSiderbarDataByMenuId(topMenuId){
    $(".main-sidebar").empty();
    var storage = window.localStorage;
	if(storage){
		var menuDatas = storage.getItem("hgtg-"+topMenuId);
		if(menuDatas){
			initSiderbarHtml(JSON.parse(menuDatas));
		}else{
			initRemoteSiderbarDataByMenuId(topMenuId);
		}
	}else{
		initRemoteSiderbarDataByMenuId(topMenuId);
	}
}

$(function(){
    //注册切换事件
    $(".sidebar-toggle").on('click', function (e) {
        e.preventDefault();
        if($(window).width()>767){
            if($("#mainCont").hasClass('sidebar-collapse')){
                $("#mainCont").removeClass('sidebar-collapse');
            }else{
                $("#mainCont").addClass('sidebar-collapse');
            }
        }else{
            if($("#mainCont").hasClass('sidebar-open')){
                $("#mainCont").removeClass('sidebar-open ');
            }else{
                $("#mainCont").addClass('sidebar-open');
            }
        }
    });
});

